package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.ProductCapability;
import com.sskj.api.exception.LogicException;
import com.sskj.api.mapper.ProductCapabilityMapper;
import com.sskj.api.service.IProductCapabilityService;
import com.sskj.api.vo.req.ProductCapabilityPageListReq;
import com.sskj.common.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ProductCapabilityService extends ServiceImpl<ProductCapabilityMapper, ProductCapability> implements IProductCapabilityService {

    @Override
    public void add(ProductCapability productCapability) {
        LambdaQueryWrapper<ProductCapability> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductCapability::getCode, productCapability.getCode())
                .or()
                .eq(ProductCapability::getName, productCapability.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicException("产品能力编码或名称已存在");
        }

        productCapability.setCreateTime(LocalDateTime.now());
        save(productCapability);
    }

    @Override
    public void update(ProductCapability productCapability) {
        ProductCapability capability = getById(productCapability.getId());
        if (capability != null) {
            capability.setName(productCapability.getName());
            capability.setStatus(productCapability.getStatus());
            capability.setDescription(productCapability.getDescription());
            capability.setUpdateTime(LocalDateTime.now());

            LambdaQueryWrapper<ProductCapability> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.ne(ProductCapability::getId, capability.getId()).and(consumer -> {
                consumer.eq(ProductCapability::getCode, capability.getCode())
                        .or()
                        .eq(ProductCapability::getName, capability.getName());
            });

            if (count(lambdaQueryWrapper) > 0) {
                throw new LogicException("产品能力编码或名称已存在");
            }

            updateById(capability);
        }
    }

    @Override
    public IPage<ProductCapability> pageList(ProductCapabilityPageListReq req) {
        LambdaQueryWrapper<ProductCapability> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(ProductCapability::getId);
        if (StrUtil.isNotBlank(req.getName())) {
            lambdaQueryWrapper.like(ProductCapability::getName, req.getName());
        }
        if (req.getStatus() != null && req.getStatus() >= 0) {
            lambdaQueryWrapper.eq(ProductCapability::getStatus, req.getStatus());
        }

        IPage<ProductCapability> pageConfig = new Page<>(req.getPageNum(), req.getPageSize());
        return page(pageConfig, lambdaQueryWrapper);
    }
}
